/*
 * @lc app=leetcode.cn id=3 lang=javascript
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
  const n = s.length;
  const ch = new Set();
  let max = 0;
  let i = 0;
  for (let j = 0; j < n; j++) {
    let cur = s[j];
    while (ch.has(cur)) {
      ch.delete(s[i]);
      i++;
    }
    ch.add(cur);
    max = Math.max(max, j - i + 1);
  }
  return max;
};
// @lc code=end
